#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
  createAction();
  createLayout();
  connectSignal();
  m_orig = new Document;
  m_note = new Document;
}

MainWindow::~MainWindow()
{
  delete m_note;
  delete m_orig;
}

void MainWindow::createLayout()
{
  QWidget *ctrWidget = new QWidget;
  m_origText = new QTextEdit;
  m_noteText = new QTextEdit;
  QHBoxLayout *textLayout = new QHBoxLayout;
  textLayout->addWidget(m_origText);
  textLayout->addWidget(m_noteText);
  ctrWidget->setLayout(textLayout);

  m_toolbar = addToolBar(tr("File"));
  m_toolbar->addAction(m_loadAct);

  setCentralWidget(ctrWidget);
}

void MainWindow::createAction()
{
  m_loadAct = new QAction("load file", this);
}

void MainWindow::connectSignal()
{
  connect(m_loadAct, SIGNAL(triggered()), this, SLOT(askAndLoad()));
}

void MainWindow::askAndLoad()
{
  fileName = QFileDialog::getOpenFileName(this, "Choose file ...");
  if (fileName.isEmpty()) return;
  if (fileName.right(4) == ".xml")
    m_orig->loadXml(fileName);
  else
    m_orig->loadTxt(fileName);
  showDocument();
}

void MainWindow::showDocument()
{
  QDomElement domElem = m_orig->documentElement();
  m_origText->setText(domElem.text());
}
